// created on 13/08/2004 at 22:51
using System;
using System.Windows.Forms;

namespace Orz.LogManager 
{
	public class FindForm : System.Windows.Forms.Form
	{
		protected System.Windows.Forms.RadioButton optWildcards;
		protected System.Windows.Forms.Button cmdFindPrev;
		protected System.Windows.Forms.GroupBox grpOptions;
		protected System.Windows.Forms.RadioButton optNoFancyStuff;
		protected System.Windows.Forms.ComboBox cboFind;
		protected System.Windows.Forms.GroupBox grpSpecialOptions;
		protected System.Windows.Forms.Label lblFind;
		protected System.Windows.Forms.CheckBox chkWholeWordsOnly;
		protected System.Windows.Forms.CheckBox chkWrapSearches;
		protected System.Windows.Forms.Button cmdCancel;
		protected System.Windows.Forms.RadioButton optRegularExpressions;
		protected System.Windows.Forms.Button cmdFindNext;
		protected System.Windows.Forms.CheckBox chkMatchCase;
		
		protected RichTextBox subject_i;
		protected Finder finder_i;
		
		
		
		
		
		
		protected FindForm()
		{
			InitializeComponent();
		}
		
		
		
		public FindForm (Finder finder_p)
		{
		    InitializeComponent();	    
		    InitialiseWithFinderDetails(finder_p);
		}
		
		
		
		void ParentFormActivated(object sender, System.EventArgs e)
		{
			if (Visible)
			{
			//	this.TopMost = true;
			//	this.Text = System.DateTime.Now.ToString();
			}
		}
		
		void ParentFormDeactivate(object sender, System.EventArgs e)
		{
			if (Visible)
			{
			//	this.TopMost = false;			
			}
		}
		
		
		
		
		public void SetSubject (Form parent_p, RichTextBox subject_p)
		{
			parent_p.Activated += new System.EventHandler(this.ParentFormActivated);
			parent_p.Deactivate += new System.EventHandler(this.ParentFormDeactivate);
			
			
			subject_i = subject_p;
			
			if (! this.Visible)
			{
				
				this.Show();				
				//MessageBox.Show (this.TopLevel + "," + this.TopMost);
				
			//	this.TopMost = true;
				//this.TopLevel = false;
			}
		}
		
		
		
		
		
		private void InitialiseWithFinderDetails (Finder finder_p)
		{
		    //
		    // Pattern combo
		    //
		    foreach (string sPattern in finder_p.PreviousPatterns)
		    {
		        cboFind.Items.Insert (0, sPattern);
		    }
		    
		    cboFind.Text = finder_p.Pattern;
		    
		    cboFind.Select (0, cboFind.Text.Length);
		    
		    
		    //
		    // Options checkboxes
		    //
		    chkMatchCase.Checked      = finder_p.MatchCase;
		    chkWholeWordsOnly.Checked = finder_p.WholeWordsOnly;
		    chkWrapSearches.Checked   = finder_p.WrapSearches;
		    
		    //
		    // Special options radio buttons
		    //
		    optNoFancyStuff.Checked = (WildcardOptions.None == finder_p.Wildcards);
		    optWildcards.Checked    = (WildcardOptions.Simple == finder_p.Wildcards);
		    optRegularExpressions.Checked = (WildcardOptions.RegularExpressions == finder_p.Wildcards);		    
		    
		    EnableButtons();
		    
		    cboFind.Focus();
		    
		    finder_i = finder_p;
		}
		
		
		
		private void EnableButtons ()
		{
		    bool bEnabled = (cboFind.Text.Length > 0);
		    cmdFindNext.Enabled = bEnabled;
		    cmdFindPrev.Enabled = bEnabled;
		}
		
		
		
		private void UpdateFinder (bool bSearchForwards_p)
		{
		    finder_i.Pattern = cboFind.Text;
		    
		    finder_i.MatchCase = chkMatchCase.Checked;
		    finder_i.WholeWordsOnly = chkWholeWordsOnly.Checked;
		    finder_i.WrapSearches = chkWrapSearches.Checked;
		    		    
		    if (optWildcards.Checked)
		    {
		        finder_i.Wildcards = WildcardOptions.Simple;
		    }
		    else if (optRegularExpressions.Checked)
		    {
		        finder_i.Wildcards = WildcardOptions.RegularExpressions;
		    }
		    else
		    {
		        finder_i.Wildcards = WildcardOptions.None;
		    }
		    
            finder_i.SearchForwards = bSearchForwards_p;
		}
		
		
		// THIS METHOD IS MAINTAINED BY THE FORM DESIGNER
		// DO NOT EDIT IT MANUALLY! YOUR CHANGES ARE LIKELY TO BE LOST
		void FindFormLoad(object sender, System.EventArgs e)
		{
			
		}
		
		protected virtual void CmdFindPrevClick(object sender, System.EventArgs e)
		{
			UpdateFinder (false);
		}
		
		protected virtual void CmdFindNextClick(object sender, System.EventArgs e)
		{
			UpdateFinder (true);
		}
		
		protected void CmdCancelClick(object sender, System.EventArgs e)
		{
			
		}
		
		void CboFindTextChanged(object sender, System.EventArgs e)
		{
		    EnableButtons();
		}
		
		void InitializeComponent() {
			this.chkMatchCase = new System.Windows.Forms.CheckBox();
			this.cmdFindNext = new System.Windows.Forms.Button();
			this.optRegularExpressions = new System.Windows.Forms.RadioButton();
			this.cmdCancel = new System.Windows.Forms.Button();
			this.chkWrapSearches = new System.Windows.Forms.CheckBox();
			this.chkWholeWordsOnly = new System.Windows.Forms.CheckBox();
			this.lblFind = new System.Windows.Forms.Label();
			this.grpSpecialOptions = new System.Windows.Forms.GroupBox();
			this.optNoFancyStuff = new System.Windows.Forms.RadioButton();
			this.optWildcards = new System.Windows.Forms.RadioButton();
			this.cboFind = new System.Windows.Forms.ComboBox();
			this.grpOptions = new System.Windows.Forms.GroupBox();
			this.cmdFindPrev = new System.Windows.Forms.Button();
			this.grpSpecialOptions.SuspendLayout();
			this.grpOptions.SuspendLayout();
			this.SuspendLayout();
			// 
			// chkMatchCase
			// 
			this.chkMatchCase.Location = new System.Drawing.Point(16, 24);
			this.chkMatchCase.Name = "chkMatchCase";
			this.chkMatchCase.Size = new System.Drawing.Size(104, 16);
			this.chkMatchCase.TabIndex = 2;
			this.chkMatchCase.Text = "&Case sensitive";
			// 
			// cmdFindNext
			// 
			this.cmdFindNext.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.cmdFindNext.Location = new System.Drawing.Point(336, 48);
			this.cmdFindNext.Name = "cmdFindNext";
			this.cmdFindNext.Size = new System.Drawing.Size(96, 24);
			this.cmdFindNext.TabIndex = 9;
			this.cmdFindNext.Text = "Find &Next";
			this.cmdFindNext.Click += new System.EventHandler(this.CmdFindNextClick);
			// 
			// optRegularExpressions
			// 
			this.optRegularExpressions.Location = new System.Drawing.Point(16, 72);
			this.optRegularExpressions.Name = "optRegularExpressions";
			this.optRegularExpressions.Size = new System.Drawing.Size(150, 16);
			this.optRegularExpressions.TabIndex = 7;
			this.optRegularExpressions.Text = "Use &regular expressions";
			// 
			// cmdCancel
			// 
			this.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmdCancel.Location = new System.Drawing.Point(336, 80);
			this.cmdCancel.Name = "cmdCancel";
			this.cmdCancel.Size = new System.Drawing.Size(96, 24);
			this.cmdCancel.TabIndex = 10;
			this.cmdCancel.Text = "Cancel";
			this.cmdCancel.Click += new System.EventHandler(this.CmdCancelClick);
			// 
			// chkWrapSearches
			// 
			this.chkWrapSearches.Location = new System.Drawing.Point(16, 72);
			this.chkWrapSearches.Name = "chkWrapSearches";
			this.chkWrapSearches.Size = new System.Drawing.Size(112, 16);
			this.chkWrapSearches.TabIndex = 4;
			this.chkWrapSearches.Text = "Wr&ap search";
			// 
			// chkWholeWordsOnly
			// 
			this.chkWholeWordsOnly.Location = new System.Drawing.Point(16, 48);
			this.chkWholeWordsOnly.Name = "chkWholeWordsOnly";
			this.chkWholeWordsOnly.Size = new System.Drawing.Size(112, 16);
			this.chkWholeWordsOnly.TabIndex = 3;
			this.chkWholeWordsOnly.Text = "&Whole words only";
			// 
			// lblFind
			// 
			this.lblFind.Location = new System.Drawing.Point(8, 16);
			this.lblFind.Name = "lblFind";
			this.lblFind.Size = new System.Drawing.Size(104, 24);
			this.lblFind.TabIndex = 0;
			this.lblFind.Text = "&Find what:";
			// 
			// grpSpecialOptions
			// 
			this.grpSpecialOptions.Controls.Add(this.optNoFancyStuff);
			this.grpSpecialOptions.Controls.Add(this.optRegularExpressions);
			this.grpSpecialOptions.Controls.Add(this.optWildcards);
			this.grpSpecialOptions.Location = new System.Drawing.Point(152, 48);
			this.grpSpecialOptions.Name = "grpSpecialOptions";
			this.grpSpecialOptions.Size = new System.Drawing.Size(168, 104);
			this.grpSpecialOptions.TabIndex = 6;
			this.grpSpecialOptions.TabStop = false;
			this.grpSpecialOptions.Text = "Pattern matching";
			// 
			// optNoFancyStuff
			// 
			this.optNoFancyStuff.Checked = true;
			this.optNoFancyStuff.Location = new System.Drawing.Point(16, 24);
			this.optNoFancyStuff.Name = "optNoFancyStuff";
			this.optNoFancyStuff.Size = new System.Drawing.Size(128, 16);
			this.optNoFancyStuff.TabIndex = 5;
			this.optNoFancyStuff.TabStop = true;
			this.optNoFancyStuff.Text = "N&o fancy stuff";
			// 
			// optWildcards
			// 
			this.optWildcards.Location = new System.Drawing.Point(16, 48);
			this.optWildcards.Name = "optWildcards";
			this.optWildcards.Size = new System.Drawing.Size(128, 16);
			this.optWildcards.TabIndex = 6;
			this.optWildcards.Text = "Use w&ildcards";
			// 
			// cboFind
			// 
			this.cboFind.Location = new System.Drawing.Point(80, 16);
			this.cboFind.Name = "cboFind";
			this.cboFind.Size = new System.Drawing.Size(240, 21);
			this.cboFind.TabIndex = 1;
			this.cboFind.TextChanged += new System.EventHandler(this.CboFindTextChanged);
			// 
			// grpOptions
			// 
			this.grpOptions.Controls.Add(this.chkWrapSearches);
			this.grpOptions.Controls.Add(this.chkWholeWordsOnly);
			this.grpOptions.Controls.Add(this.chkMatchCase);
			this.grpOptions.Location = new System.Drawing.Point(8, 48);
			this.grpOptions.Name = "grpOptions";
			this.grpOptions.Size = new System.Drawing.Size(136, 104);
			this.grpOptions.TabIndex = 4;
			this.grpOptions.TabStop = false;
			this.grpOptions.Text = "Options";
			// 
			// cmdFindPrev
			// 
			this.cmdFindPrev.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.cmdFindPrev.Location = new System.Drawing.Point(336, 16);
			this.cmdFindPrev.Name = "cmdFindPrev";
			this.cmdFindPrev.Size = new System.Drawing.Size(96, 24);
			this.cmdFindPrev.TabIndex = 8;
			this.cmdFindPrev.Text = "Find &Previous";
			this.cmdFindPrev.Click += new System.EventHandler(this.CmdFindPrevClick);
			// 
			// FindForm
			// 
			this.AcceptButton = this.cmdFindPrev;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.cmdCancel;
			this.ClientSize = new System.Drawing.Size(442, 160);
			this.Controls.Add(this.grpSpecialOptions);
			this.Controls.Add(this.cmdFindPrev);
			this.Controls.Add(this.grpOptions);
			this.Controls.Add(this.cmdCancel);
			this.Controls.Add(this.cmdFindNext);
			this.Controls.Add(this.cboFind);
			this.Controls.Add(this.lblFind);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "FindForm";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Find";
			this.Load += new System.EventHandler(this.FindFormLoad);
			this.grpSpecialOptions.ResumeLayout(false);
			this.grpOptions.ResumeLayout(false);
			this.ResumeLayout(false);
		}
	}
}
